package lch.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import lch.dao.OrderSettingDao;
import lch.pojo.OrderSetting;
import lch.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author lch
 * @date 2019-12-23 11:17
 */
@Transactional
@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> data) {
        for (OrderSetting orderSetting : data) {
            Date orderDate = orderSetting.getOrderDate();
            long countByOrderDate = orderSettingDao.findCountByOrderDate(orderDate);
            if (countByOrderDate > 0) {
                orderSettingDao.editNumberByOrderDate(orderSetting);
            } else {
                orderSettingDao.add(orderSetting);
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String, Object> objectObjectMap = new HashMap<>();
        objectObjectMap.put("begin", begin);
        objectObjectMap.put("end", end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(objectObjectMap);
        List<Map> result = new ArrayList<Map>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map<String, Object> map = new HashMap<String, Object>();
                int day = orderSetting.getOrderDate().getDate();
                int number = orderSetting.getNumber();
                int reservations = orderSetting.getReservations();
                map.put("date", day);
                map.put("number", number);
                map.put("reservations", reservations);
                result.add(map);
            }
        }
        return result;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        long countByOrderDate = orderSettingDao.findCountByOrderDate(orderDate);
        if (countByOrderDate > 0) {
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            orderSettingDao.add(orderSetting);
        }
    }
}